﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookMGI
{
    public partial class ThisAddIn
    {
        Office.CommandBarButton btn1;
        Office.CommandBarButton button;
        Outlook.Selection selection;
        Outlook.MailItem item;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.Explorer explorer = this.Application.ActiveExplorer();
            if (explorer != null)
            {
                Office.CommandBar bar = explorer.CommandBars.Add("MGI", 1, missing, true);
                bar.Visible = true;
                btn1 = (Office.CommandBarButton)bar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                btn1.Caption = "Alta Incidente";
                btn1.Tag = "myOutlookAddin1.btn1";
                btn1.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                btn1.FaceId = 215;
                btn1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Btn1_Click);

                Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
                Application.ContextMenuClose += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ContextMenuCloseEventHandler(Application_ContextMenuClose);
            }
        }

        private void Btn1_Click(Office.CommandBarButton Ctrl, ref bool Cancel)
        {
            selection = this.Application.ActiveExplorer().Selection;
            if (GetMessageClass(selection[1]) == "IPM.Note" && selection.Count == 1)
                try
                {
                    item = (Outlook.MailItem)selection[1];
                }
                catch (Exception)
                {
                    item = null;
                }

            if (item != null)
            {
                item.Display(false);
                item.GetInspector.SetCurrentFormPage("OutlookMGI.IncidentesGUI");
            }

            selection = null;
            item = null;
        }

        void Application_ContextMenuClose(Microsoft.Office.Interop.Outlook.OlContextMenu ContextMenu)
        {
            selection = null;
            item = null;
            if (button != null)
                button.Click -= new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Btn1_Click);
            button = null;
        }

        void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
        {
            selection = this.Application.ActiveExplorer().Selection;
            if (GetMessageClass(selection[1]) == "IPM.Note" && selection.Count == 1)
            {
                item = (Outlook.MailItem)selection[1];
                button = (Office.CommandBarButton)CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, missing);
                button.Caption = "Alta Incidente";
                button.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                button.FaceId = 215;
                button.Visible = true;
                button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Btn1_Click);
            }
        }

        private string GetMessageClass(object item)
        {
            object[] args = new Object[] { };
            Type t = item.GetType();
            return t.InvokeMember("messageClass",
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.GetProperty,
                null,
                item,
                args).ToString();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            Application.ItemContextMenuDisplay -= new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
            Application.ContextMenuClose -= new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ContextMenuCloseEventHandler(Application_ContextMenuClose);

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
